<?php

class User extends Admin_Controller {

	public function __construct()
	{
		parent::__construct();
		$this->load->model('user_model');
	}

	//普通会员
	public function member()
	{
		$p = intval(segment(4));
		$p = $p ? $p : 1;
		$per_page = 20;
		$condition = array('is_admin'=>0);
		$count = $this->user_model->get_count($condition);
		$user_list = $this->user_model->get_page($p, $per_page, $condition);
		$this->load->library('page');
		$config = array(
			'base_url' => admin_url('user/index'),
			'total_rows' => $count,
			'current_page' => $p,
			'per_page' => $per_page
		);
		$this->page->init($config);
		
		$this->template->assign('list', $user_list);
		$this->template->assign('page', $this->page);
		$this->template->assign('title', '会员');
		$this->template->display('user_list');
	}

	//管理员
	public function admin()
	{
		$p = intval(segment(4));
		$p = $p ? $p : 1;
		
		$per_page = 20;

		$condition = array('is_admin'=>1);
		$count = $this->user_model->get_count($condition);
		$user_list = $this->user_model->get_page($p, $per_page, $condition);
		$this->load->library('page');
		$config = array(
			'base_url' => admin_url('user/admin'),
			'total_rows' => $count,
			'current_page' => $p,
			'per_page' => $per_page
		);
		$this->page->init($config);
		
		$this->template->assign('list', $user_list);
		$this->template->assign('page', $this->page->show());
		$this->template->assign('title', '管理员');
		$this->template->display('user_list');
	}
	
	protected function _insert_get()
	{
		$this->load->model('role_model');
		$role_list = $this->role_model->get_all(array('parent_id !='=>0));
		$this->template->assign('list', $role_list);
		$this->template->display('user_insert');
	}
	
	protected function _insert_post()
	{
		$this->load->library('validate');
		$rules = array(
			array('username', '用户名', 'required|letter_first|mix|min_length[5]|max_length[20]'),
			array('password', '密码', 'required|mix|min_length[5]|max_length[20]'),
			array('email', '邮箱', 'required|email'),
			array('role_id', '角色', 'required|natural')
		);
		if (FALSE==$this->validate->run($rules))
		{
			msg($this->validate->error(), -1);
		}

		$username = post('username');
		$password = post('password');
		$email = post('email');
		$role_id = post('role_id');
		
		$this->load->model('role_model');
		if ( ! $this->role_model->exist($role_id))
		{
			msg('角色不存在', -1);
		}
	
		if ($this->user_model->get_one(array('username'=>$username)))
		{
			msg('用户名已经被注册', -1);
		}

		if ($this->user_model->get_one(array('email'=>$email)))
		{
			msg('邮箱已经被注册', -1);
		}
		
		$this->load->library('sauth');
		
		$password = $this->sauth->password_hash($password);
		
		$data = array(
			'username' => $username,
			'password' => $password,
			'email' => $email,
			'role_id' => $role_id
		);
		
		if (FALSE===$this->user_model->insert($data))
		{
			msg('添加失败');
		}
		
		msg('添加成功', admin_url('user/index'));
	}
	
	protected function _update_get()
	{
		$id = intval(segment(4));
		if ( ! $id)
		{
			show_error('非法操作');
		}
		
		$user_data = $this->user_model->get($id);
		if ( ! $user_data)
		{
			msg('用户不存在或已被删除', -1);
		}
		
		$this->load->model('role_model');
		$role_list = $this->role_model->get_all(array('parent_id !='=>0));
		$this->template->assign('list', $role_list);
		
		$this->template->assign('data', $user_data);
		$this->template->display('user_update');
	}
	
	protected function _update_post()
	{
		$id = post('id', TRUE);
		$password = post('password', TRUE);
		$rpassword = post('rpassword', TRUE);
		$email = post('email', TRUE);
		$role_id = post('role_id', TRUE);
		
		if ($password!=$rpassword)
		{
			msg('两次密码不一致，请重新输入', -1);
		}
		
		$user_data = $this->user_model->get($id);
		if ( ! $user_data)
		{
			msg('用户不存在或已被删除', admin_url('user/index'));
		}
		
		$data = array();
		
		if ($email)
		{
			if ($email!=$user_data['email'] && $this->user_model->get_one(array('email'=>$email)))
			{
				msg('邮箱已经被注册', -1);
			}
			$data['email'] = $email;
		}
		
		if ($role_id!=$user_data['role_id'])
		{
			$this->load->model('role_model');
			if ( ! $this->role_model->exist($role_id))
			{
				msg('角色不存在或已被删除', -1);
			}
			$data['role_id'] = $role_id;
		}

		if ($password)
		{
			$this->load->library('sauth');
			$password = $this->sauth->password_hash($password);
			if ($password!=$user_data['password'])
			{
				$data['password'] = $password;
			}
			else
			{
				unset($password);
			}
		}
		else
		{
			unset($password);
		}
		
		if ($data && FALSE===$this->user_model->update($data, $id))
		{
			msg('修改失败', -1);
		}
		
		if (isset($password))
		{
			if ($id==$this->sauth->get_user_id())
			{
				$this->sauth->logout();
				msg('您的密码已经被修改，请重新登录', admin_url('welcome/login'));	
			}
		}
		
		msg('修改成功', admin_url('user/index'));
	}
	
}